package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.NutsNet;
import com.sls.web.manager.game.pojo.NutsNetOrder;
import com.sls.web.manager.game.querybean.NutsNetOrderQuery;
import com.sls.web.manager.game.querybean.NutsNetQuery;
import com.sls.web.manager.game.service.NutsNetService;
import com.sls.web.manager.page.Pagination;

public class NutsNetAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	@Resource(name = "nutsNetService")
	private NutsNetService nutsNetService;
	private String supProductId;
	private NutsNet nutsNet;
	private NutsNetQuery nutsNetQuery;
	private Pagination<?> page;
	private NutsNetOrder nutsNetOrder;
	private String ptOrderNo;
	private NutsNetOrderQuery nutsNetOrderQuery;
	private List<NutsNet> supUserIdList;
	private int priceRatio;
	private String supplierId;

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (nutsNetQuery == null) {
				nutsNetQuery = new NutsNetQuery();
				nutsNetQuery.setPageSize(Constants.INIT_PAGESIZE);
				nutsNetQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String supProductId = nutsNetQuery.getSupProductId();
			String name = nutsNetQuery.getName();
			supProductId = FilterStringSpaces.checkStringHasSpace(supProductId);
			name = FilterStringSpaces.checkStringHasSpace(name);
			nutsNetQuery.setSupProductId(supProductId);
			nutsNetQuery.setName(name);
			page = nutsNetService.queryNutsNet(nutsNetQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 坚果商品审核
	 * 
	 * @return
	 * @throws Exception
	 */
	public String audit() throws Exception {
		try {
			this.nutsNetService.confirmAudit(supplierId, priceRatio,
					getCurrentOperator());
			redirectionUrl = "nuts_net!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	/**
	 * 详情查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		try {
			nutsNet = nutsNetService.queryNutsNetById(supProductId,
					getCurrentOperator());
			if (nutsNet == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			return ERROR;
		}
		return "detail";
	}

	public String toBalance() throws Exception {
		supUserIdList = nutsNetService.querySupUserId(nutsNet,
				getCurrentOperator());
		return "balance";
	}

	/**
	 * 坚果账户余额
	 * 
	 * @return
	 * @throws Exception
	 */
	public String balance() throws Exception {
		try {
			supUserIdList = nutsNetService.querySupUserId(nutsNet,
					getCurrentOperator());
			String supUserId = nutsNetQuery.getSupUserId();
			if (StringUtils.isNotEmpty(supUserId)) {
				supUserId = supUserId.trim();
			}
			nutsNetQuery.setSupUserId(supUserId);
			nutsNet = nutsNetService.queryBalance(nutsNetQuery,
					getCurrentOperator());
			if ("100".equals(nutsNet.getFailedCode())) {
				flag = false;
				addActionMessage(nutsNet.getFailedReason());
			} else if ("101".equals(nutsNet.getFailedCode())) {
				flag = false;
				addActionMessage(nutsNet.getFailedReason());
			} else if ("102".equals(nutsNet.getFailedCode())) {
				flag = false;
				addActionMessage(nutsNet.getFailedReason());
			} else if ("103".equals(nutsNet.getFailedCode())) {
				flag = false;
				addActionMessage(nutsNet.getFailedReason());
			} else {
				return "balance";
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "balance";
	}

	public String toOrder() throws Exception {
		return "order_list";
	}

	/**
	 * 订单列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String orderList() throws Exception {
		try {
			String ptOrderNo = nutsNetOrderQuery.getPtOrderNo();
			String supOrderNo = nutsNetOrderQuery.getSupOrderNo();
			ptOrderNo = FilterStringSpaces.checkStringHasSpace(ptOrderNo);
			supOrderNo = FilterStringSpaces.checkStringHasSpace(supOrderNo);
			nutsNetOrderQuery.setPtOrderNo(ptOrderNo);
			nutsNetOrderQuery.setSupOrderNo(supOrderNo);
			nutsNetOrder = nutsNetService.queryOrder(nutsNetOrderQuery,
					getCurrentOperator());
			return "order_list";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 订单详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String orderDetail() throws Exception {
		try {
			nutsNetOrder = nutsNetService.queryOrderById(ptOrderNo,
					getCurrentOperator());
			if (nutsNetOrder == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		return "order_detail";
	}

	/**
	 * 商品详细信息同步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String goodsDetailSync() throws Exception {
		try {
			this.nutsNetService.goodsDetailSync(getCurrentOperator());
			redirectionUrl = "nuts_net!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	/**
	 * 批量上架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOnline() throws Exception {
		try {
			this.nutsNetService.batchOnline("1", getCurrentOperator());
			redirectionUrl = "nuts_net!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 批量下架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOffline() throws Exception {
		try {
			this.nutsNetService.batchOffline("0", getCurrentOperator());
			redirectionUrl = "nuts_net!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public String getSupProductId() {
		return supProductId;
	}

	public void setSupProductId(String supProductId) {
		this.supProductId = supProductId;
	}

	public NutsNet getNutsNet() {
		return nutsNet;
	}

	public void setNutsNet(NutsNet nutsNet) {
		this.nutsNet = nutsNet;
	}

	public NutsNetQuery getNutsNetQuery() {
		return nutsNetQuery;
	}

	public void setNutsNetQuery(NutsNetQuery nutsNetQuery) {
		this.nutsNetQuery = nutsNetQuery;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public NutsNetService getNutsNetService() {
		return nutsNetService;
	}

	public void setNutsNetService(NutsNetService nutsNetService) {
		this.nutsNetService = nutsNetService;
	}

	public NutsNetOrder getNutsNetOrder() {
		return nutsNetOrder;
	}

	public void setNutsNetOrder(NutsNetOrder nutsNetOrder) {
		this.nutsNetOrder = nutsNetOrder;
	}

	public String getPtOrderNo() {
		return ptOrderNo;
	}

	public void setPtOrderNo(String ptOrderNo) {
		this.ptOrderNo = ptOrderNo;
	}

	public NutsNetOrderQuery getNutsNetOrderQuery() {
		return nutsNetOrderQuery;
	}

	public void setNutsNetOrderQuery(NutsNetOrderQuery nutsNetOrderQuery) {
		this.nutsNetOrderQuery = nutsNetOrderQuery;
	}

	public List<NutsNet> getSupUserIdList() {
		return supUserIdList;
	}

	public void setSupUserIdList(List<NutsNet> supUserIdList) {
		this.supUserIdList = supUserIdList;
	}

	public int getPriceRatio() {
		return priceRatio;
	}

	public void setPriceRatio(int priceRatio) {
		this.priceRatio = priceRatio;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

}
